package com.lipaluma.csv.io;

import com.lipaluma.exceptions.CsvParserException;
import com.lipaluma.field.mapper.mapping.Column;
import com.lipaluma.field.mapper.mapping.MappingInfo;
import com.lipaluma.io.Row;

/**
 * Row from a Csv File used for reading
 * 
 * @author Mario
 */
public class CsvReadRow implements Row {

	private String[] line;
	
	/**
	 * create a csv row for reading
	 * @param line array of values of the csv line
	 */
	public CsvReadRow(String[] line) {
		this.line = line;
	}
	
	public String extractValueOnIndex(int index) {
		return line[index];
	}

	public void writeValueOnRow(Object value, int index) {
		throw new CsvParserException("The class CsvReadRow is not applicable for writing values on line");
	}

	@Override
	public String extractValue(MappingInfo info) {
		Column column = (Column)info;
		if(column.getColumnIndex() == null)
			throw new CsvParserException("The column name '"+column.getColumnName()+"' wasn't found during mapping headers.");
		return line[column.getColumnIndex()];
	}

	@Override
	public void writeValue(Object value, MappingInfo info) {
		throw new CsvParserException("The class CsvReadRow is not applicable for writing values on line");
	}
	
	@Override
	public String[] toArray() {
		return line;
	}
}
